package com.smartfood;


import com.smartfood.R;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;


public class Timer extends Activity implements OnClickListener {
	private Button startButton;
	private Button stopButton;
	private Button reinitializeButton;
	private Button incHoursButton;
	private Button decHoursButton;
	private Button incMinutesButton;
	private Button decMinutesButton;
	private Button incSecondsButton;
	private Button decSecondsButton;
	
	private MediaPlayer myMediaPLayer;
	private TextView timeDisplay;
	private MyCount counter;
	

	static boolean isRunning = false;
	static long startTime = 0;
	static long currentTime = 0;
	static long setTime = 0;
	static long elapsedTime = 0;
	static long remainingTime = 0;
	
	//static Bundle bundleState;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// get button
		setContentView(R.layout.activity_timer);
		

			
			        
		startButton=(Button)findViewById(R.id.button1);
		startButton.setOnClickListener(this);
		startButton.setVisibility(View.VISIBLE);
		stopButton=(Button)findViewById(R.id.button2);
		stopButton.setOnClickListener(this);
		stopButton.setVisibility(View.INVISIBLE);
		reinitializeButton=(Button)findViewById(R.id.button3);
		reinitializeButton.setOnClickListener(this);
		incHoursButton=(Button)findViewById(R.id.button4);
		incHoursButton.setOnClickListener(this);
		decHoursButton=(Button)findViewById(R.id.button5);
		decHoursButton.setOnClickListener(this);
		incMinutesButton=(Button)findViewById(R.id.button6);
		incMinutesButton.setOnClickListener(this);
		decMinutesButton=(Button)findViewById(R.id.button7);
		decMinutesButton.setOnClickListener(this);
		incSecondsButton=(Button)findViewById(R.id.button8);
		incSecondsButton.setOnClickListener(this);
		decSecondsButton=(Button)findViewById(R.id.button9);
		decSecondsButton.setOnClickListener(this);
		
		// btnBack
		Button btnBack=(Button)findViewById(R.id.back);
		btnBack.setOnClickListener(new OnClickListener() {
			//@Override
			public void onClick(View v) {
				Intent home=new Intent(Timer.this,Smartfood.class);
				startActivity(home);
			}
		});
		
		timeDisplay=(TextView)findViewById(R.id.timer);
			
		counter=new MyCount(setTime, 100);
	}

	//@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.button1: //startButton pressed -> Start or Resume
			if(setTime > 0){
				counter.cancel();
		        timeDisplay.setText(formatTime(setTime));
		        counter=new MyCount(setTime, 100);
				startTime = System.currentTimeMillis();
				counter.start();
				startButton.setVisibility(View.INVISIBLE);
				stopButton.setVisibility(View.VISIBLE);
				isRunning = true;
			}
			break;
		case R.id.button2: //stopButton pressed -> Pause
			currentTime = System.currentTimeMillis();
			elapsedTime = currentTime - startTime;
			remainingTime = setTime - elapsedTime;
			counter.cancel();
			counter=new MyCount(remainingTime, 100);
			stopButton.setVisibility(View.INVISIBLE);
			startButton.setVisibility(View.VISIBLE);
			isRunning = false;
			break;
		case R.id.button3: //reinitializeButton pressed -> Stop and Reinitialization
			counter.cancel();
			counter=new MyCount(setTime, 100);
			timeDisplay.setText(formatTime(setTime));
			stopButton.setVisibility(View.INVISIBLE);
			startButton.setVisibility(View.VISIBLE);
			isRunning = false;
			break;
		case R.id.button4: // plus hours
			if(!isRunning){
				if(setTime >= 11*3600000){
					setTime = setTime%3600000;
				} else {
					setTime += 3600000;
				}
			}
			timeDisplay.setText(formatTime(setTime));
			break;
		case R.id.button5: // minus hours
			if(!isRunning){
				if(setTime <= 3600000){
					setTime += 11*3600000;
				} else {
					setTime -= 3600000;
				}
			}
			timeDisplay.setText(formatTime(setTime));
			break;
		case R.id.button6: // plus minutes
			if(!isRunning){
				if((setTime%3600000) >= 59*60000){
					setTime = (setTime-setTime%3600000) + setTime%60000;
				} else {
					setTime += 60000;
				}
			}
			timeDisplay.setText(formatTime(setTime));
			break;
		case R.id.button7: // minus minutes
			if(!isRunning){
				if(setTime%3600000 <= 60000){
					setTime = (setTime-setTime%3600000) + 59*60000 + setTime%60000;
				} else {
					setTime -= 60000;
				}
			}
			timeDisplay.setText(formatTime(setTime));
			break;
		case R.id.button8: // plus secondes
			if(!isRunning){
				if((setTime%60000) == 59*1000){
					setTime = (setTime-(setTime%60000)) + 1000;
				} else {
					setTime +=1000;
				}
			}
			timeDisplay.setText(formatTime(setTime));
			break;
		case R.id.button9: //minus secondes
			if(!isRunning){
				if((setTime%60000) == 1000){
					setTime = (setTime-(setTime%60000))+59*1000;
				} else {
					setTime -= 1000;
				}
			}
			timeDisplay.setText(formatTime(setTime));
			break;
		default:
			break;
		}
	}

	public class MyCount extends CountDownTimer {

		public MyCount(long millisInFuture, long countDownInterval){
			super(millisInFuture, countDownInterval);
		}

		public void onFinish() {
			playAlarm();
			//timeDisplay.setText("Done!");
			counter.cancel();
			counter=new MyCount(setTime, 100);
			isRunning=false;
			timeDisplay.setText(formatTime(setTime));
			stopButton.setVisibility(View.INVISIBLE);
			startButton.setVisibility(View.VISIBLE);
		}

		public void onTick(long millisUntilFinished) {
			//timeDisplay.setText("Left: " + millisUntilFinished/100);
			timeDisplay.setText(formatTime(millisUntilFinished));
		}
	}

	public String formatTime (long millis){
		String output = "00:00:00";
		long seconds = millis / 1000;
		long minutes = seconds / 60;
		long hours = minutes / 60;

		seconds = seconds % 60;
		minutes = minutes % 60;
		hours = hours % 60;

		String secondsD = String.valueOf(seconds);
		String minutesD = String.valueOf(minutes);
		String hoursD = String.valueOf(hours);

		if (seconds < 10)
			secondsD = "0" + seconds;
		if (minutes < 10)
			minutesD = "0" + minutes;
		if (hours < 10)
			hoursD = "0" + hours;

		output = hoursD + " : " + minutesD + " : " + secondsD;
		return output;
	}

	public void playAlarm() {
		// http://www.soundjay.com/beep-sounds-1.html lots of free beeps here
		myMediaPLayer = MediaPlayer.create(this,R.raw.beep9);
		myMediaPLayer.setLooping(false);
		myMediaPLayer.start();
		myMediaPLayer.setOnCompletionListener(new OnCompletionListener() {
			public void onCompletion(MediaPlayer arg0) {
				// TODO Auto-generated method stub
				//finish();
			}
		});

	}
	
	@Override
	protected void onDestroy(){
		super.onDestroy();
		if (myMediaPLayer != null){
			myMediaPLayer.release();
			myMediaPLayer = null;
		}
	}
}